#include "caiimplatforminputcontext.h"
#include <QDebug>
#include <QWidget>
#include <QKeyEvent>
#include <QGuiApplication>

caiImPlatformInputContextPrivate::caiImPlatformInputContextPrivate() : m_pKeyBoard(0)
{

}

caiImPlatformInputContextPrivate::~caiImPlatformInputContextPrivate()
{
    if(m_pKeyBoard)
    {
        delete m_pKeyBoard;
    }
}


caiImPlatformInputContext::caiImPlatformInputContext() :
    m_focusObject(NULL)
{
    connect(context->m_pKeyBoard, SIGNAL(commit(QString)), this, SLOT(keyboardCommit(QString)));
    connect(context->m_pKeyBoard, SIGNAL(SendkeyEvent(QEvent*)), this, SLOT(sendEvent(QEvent*)));

}

caiImPlatformInputContext::~caiImPlatformInputContext()
{

}

bool caiImPlatformInputContext::isValid() const
{
    return true;
}

caiImPlatformInputContextPrivate *caiImPlatformInputContext::context = new caiImPlatformInputContextPrivate();

void caiImPlatformInputContext::setFocusObject(QObject *object)
{
   qDebug()<< "caiImPlatformInputContext::setFocusObject():" << object;

   if(!object)
   {
       return;
   }

    if (m_focusObject != object) {//如果焦点控件变更
        if (m_focusObject)
            m_focusObject->removeEventFilter(this);
        m_focusObject = object;
        if (m_focusObject)
            m_focusObject->installEventFilter(this);
    }

}

void caiImPlatformInputContext::showInputPanel()
{    
    // 获取焦点控件大小
    QWidget *w = qobject_cast<QWidget*>(m_focusObject);
    QPoint pt = w->pos();
    QRect rect = w->rect();
    pt = w->mapToGlobal(QPoint(0, 0));

    // 如果键盘已经创建过 则删除，重新创建
    /*if(d->m_pKeyBoard)
    {
        delete d->m_pKeyBoard;
        d->m_pKeyBoard = 0;
    }*/

    if(!context->m_pKeyBoard)
    {
        qDebug() << "new m_pKeyBoard";
        context->m_pKeyBoard = new KeyBoard(w);
        connect(context->m_pKeyBoard, SIGNAL(commit(QString)), this, SLOT(keyboardCommit(QString)));
        connect(context->m_pKeyBoard, SIGNAL(SendkeyEvent(QEvent*)), this, SLOT(sendEvent(QEvent*)));

    }

    //显示键盘
    context->m_pKeyBoard->showKeyboard(pt, rect);

    // 获取焦点窗口，用于移动的对象
    const QGuiApplication *app = qApp;
    QWindow *focusWindow = app ? app->focusWindow() : 0;

    m_pFocusWindow = focusWindow;

}

void caiImPlatformInputContext::hideInputPanel()
{
    if(context->m_pKeyBoard)
    {
        context->m_pKeyBoard->hide();
    }
}

bool caiImPlatformInputContext::isInputPanelVisible() const
{
    return false;
}

// 按键消息
void caiImPlatformInputContext::sendEvent(QEvent *evnet)
{
    if (m_focusObject == NULL)
    {
        return;
    }

    QGuiApplication::sendEvent(m_focusObject, evnet);
}

// 接受键盘消息
void caiImPlatformInputContext::keyboardCommit(QString str)
{
    if (m_focusObject == NULL)
    {
        return;
    }

    QInputMethodEvent event;
    event.setCommitString(str);

    QGuiApplication::sendEvent(m_focusObject, &event);
}
bool caiImPlatformInputContext::eventFilter(QObject *watched, QEvent *event)
{
    const QKeyEvent *keyEvent = (const QKeyEvent *)event;
    int key = keyEvent->key();
    // should pass only the key presses
    if (keyEvent->type() != QEvent::KeyPress) {
        return false;
    }

    if (key == Qt::Key_Left || key == Qt::Key_Right ||
            key == Qt::Key_Up || key == Qt::Key_Down ||
            key == Qt::Key_Tab)
    {
        return false;
    }
    else if(key ==Qt::Key_Escape)
    {
        hideInputPanel();
        return true;
    }

    qDebug() << "filter Event" << endl;
    //if (!isInputPanelVisible())
    //{
    //    showInputPanel();
    //}
    return false;
}




